package chat

import (
	"context"

	"gitee.com/allowall-package/goutil/fn"
	micro "github.com/micro/go-micro/v2"
)

// 服务
type chat struct{}

func (c chat) Text(ctx context.Context, in *ChatRequest, res *ChatResponse) error {
	res.Res = "Text message: " + in.Text
	return nil
}

func (c chat) Voice(ctx context.Context, in *ChatRequest, res *ChatResponse) error {
	res.Res = "Voice message: " + in.Text
	return nil
}

var Chat = new(chat)

func MicroServerRun() error {
	// Create a new service. Optionally include some options here.
	service := micro.NewService(
		micro.Name("chat"),
	)

	// Init will parse the command line flags.
	service.Init()

	// Register handler
	RegisterChatHandler(service.Server(), Chat)

	// Run the server
	return service.Run()
}

// 用带参数的命令行请求有可能会报错
func MicroClientCall() {
	// Create a new service
	service := micro.NewService(micro.Name("chat.client"))
	// Initialise the client and parse command line flags
	service.Init()

	// Create new chat client
	c := NewChatService("chat", service.Client())

	// Call the text
	resT, _ := c.Text(context.TODO(), &ChatRequest{Text: "Call aaa"})

	// Call the voice
	resV, _ := c.Voice(context.TODO(), &ChatRequest{Text: "Call bbb"})

	fn.Dump(fn.FuncInfo(), resT, resV)
}
